package com.example.culturapp;

import java.util.ArrayList;
import java.util.List;

import android.app.Activity;
import android.content.Intent;
import android.content.pm.ResolveInfo;
import android.os.Parcelable;
import android.view.View;
import android.view.View.OnClickListener;

public class MostrarEventoControler implements OnClickListener{
	private final View myView;
	private ItemHoy evento;
	private final Activity myActivity;
	
	public MostrarEventoControler(ItemHoy evento,View v, Activity activity) {
    	myView = v;
    	myActivity = activity;
        this.evento = evento;
    }
    
    private Intent getShareIntent(String type, String subject, String text) 
    {
        boolean found = false;
        Intent share = new Intent(android.content.Intent.ACTION_SEND);
        share.setType("text/plain");

        // gets the list of intents that can be loaded.
        List<ResolveInfo> resInfo = myActivity.getPackageManager().queryIntentActivities(share, 0);
        if (!resInfo.isEmpty()){
            for (ResolveInfo info : resInfo) {
                if (info.activityInfo.packageName.toLowerCase().contains(type) || 
                        info.activityInfo.name.toLowerCase().contains(type) ) {
                    share.putExtra(Intent.EXTRA_SUBJECT,  subject);
                    share.putExtra(Intent.EXTRA_TEXT,     text);
                    share.setPackage(info.activityInfo.packageName);
                    found = true;
                    break;
                }
            }
            if (!found)
                return null;

            return share;
        }
        return null;
    }
    
    public void onClick(View v) {
    	
    	switch (v.getId()) {
		case R.id.tw:
			List<Intent> targetedShareIntents = new ArrayList<Intent>();
			Intent twitterIntent = getShareIntent("twitter", evento.getNombre(),"#VamosA "+"#"+evento.getNombre().replace(" ", "")+" "+evento.getFecha()+" "+evento.getHora()+"hs "+evento.getLugar()+"-"+evento.getDescripcion()+" #CulturApp");
			 if(twitterIntent != null)
			 targetedShareIntents.add(twitterIntent);
			 Intent chooser = Intent.createChooser(targetedShareIntents.remove(0), "Delen");

			    chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));

			    myActivity.startActivity(chooser);
			break;
		case R.id.fb:
			targetedShareIntents = new ArrayList<Intent>();
			Intent facebookIntent = getShareIntent("facebook", evento.getNombre(),"#VamosA "+"#"+evento.getNombre().replace(" ", "")+" "+evento.getFecha()+" "+evento.getHora()+"hs "+evento.getLugar()+"-"+evento.getDescripcion()+" #CulturApp");
			 if(facebookIntent != null){
				 targetedShareIntents.add(facebookIntent);
				 facebookIntent.putExtra(Intent.EXTRA_TEXT, "http://www.google.com/ Check out my new app...");
				 facebookIntent.putExtra(Intent.EXTRA_TEXT, "Check out my new app...");
			 }
			 chooser = Intent.createChooser(targetedShareIntents.remove(0), "Delen");

			    chooser.putExtra(Intent.EXTRA_INITIAL_INTENTS, targetedShareIntents.toArray(new Parcelable[]{}));

			    myActivity.startActivity(chooser);
			break;
			
		case R.id.go:
			break;
			
		default:
			break;
		}
			
			
    }
}
